package compare;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

/**
 * Created by CXinZhi on 2016/10/20.
 */
public class ChangeExcelName implements ChangeName {


	private boolean isChange;

	private StringBuilder matchMeg;

	public ChangeExcelName(String path, String excelName, String pName, String cName, String cPath) {
		this.path = path;
		this.cName = cName;
		this.pName = pName;
		this.cPath = cPath;
		isChange = false;
		this.excelName = excelName;
		matchMeg = new StringBuilder();
	}


	//地址
	private String path;

	//旧名称
	private String pName;

	//新名称
	private String cName;

	//新名称
	private String cPath;

	private String excelName;


	@Override
	public boolean doChange() throws IOException {

		if (!path.contains("xls"))
			return false;

		FileInputStream stream = null;
		FileOutputStream outputStream = null;

		try {
			// 加载文件
			stream = new FileInputStream(new File(path));
			// 初始化 excel 文件
			Workbook workbook = new HSSFWorkbook(stream);
			Iterator<Sheet> sheetIterator = workbook.sheetIterator();
			int n = 0;
			if (sheetIterator.hasNext()) {
				HSSFSheet sheet = (HSSFSheet) sheetIterator.next();
				int firstRow1 = sheet.getFirstRowNum();
				int lastRow1 = sheet.getLastRowNum();

				workbook.setSheetName(n, sheet.getSheetName().replaceAll(pName, cName));

				for (int i = firstRow1; i <= lastRow1; i++) {
					//	System.out.println("\n\nComparing Row "+i);
					HSSFRow row = sheet.getRow(i);
					int cellNum = row.getFirstCellNum();
					int lastCellNum = row.getLastCellNum();
					for (int j = cellNum; j < lastCellNum; j++) {
						HSSFCell cell = row.getCell(j);
						if (cell != null) {
							String cellValue = cell.getStringCellValue();
							if (cellValue.contains(pName)) {
								matchMeg.append(String.format("%s 替换文件路径1:[%s] \n", "	", path));
								cell.setCellValue(cellValue.replaceAll(pName, cName));
								getReplaceInfo(sheet, row);
								isChange = true;
							}

						}
					}
				}
				n++;
			}
			outputStream = new FileOutputStream(cPath + "/" + excelName.replaceAll(pName, cName));
			workbook.write(outputStream);
			workbook.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			stream.close();
			outputStream.flush();
			outputStream.close();
		}
		return false;
	}

	public String getChange() {
		if (isChange)
			return "1";
		else
			return "0";
	}

	private void getReplaceInfo(HSSFSheet sheet, HSSFRow row) {

		HSSFRow titleRow = sheet.getRow(0);
		int cellNum = titleRow.getFirstCellNum();
		int lastCellNum = titleRow.getLastCellNum();
		for (int j = cellNum; j < lastCellNum; j++) {
			try {
				HSSFCell cell = titleRow.getCell(j);
				matchMeg.append("             " + cell.getStringCellValue());
			}catch (Exception ex){}
		}
		matchMeg.append("\r\n");

		cellNum = row.getFirstCellNum();
		lastCellNum = row.getLastCellNum();
		for (int j = cellNum; j < lastCellNum; j++) {
			try {
			HSSFCell cell = row.getCell(j);
			matchMeg.append("             "+cell.getStringCellValue());
			}catch (Exception ex){}
		}
		matchMeg.append("\r\n");
	}

	public StringBuilder getMatchMeg() {
		return matchMeg;
	}

	public void setMatchMeg(StringBuilder matchMeg) {
		this.matchMeg = matchMeg;
	}

	public boolean isChange() {
		return isChange;
	}

	public void setChange(boolean change) {
		isChange = change;
	}

}
